home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / codebrk.exe / WCDBRK2.C < prev    next >
C/C++ Source or Header  |  1991-06-07  |  17KB  |  677 lines

  1. /*********************************************************************
  2.     PROGRAM: WCDBRK2.C
  3.  
  4.     PURPOSE: Code Break game
  5.  
  6.     AUTHOR:  Ken Fogel
  7.          Omnibus Systems
  8.          8108 Norfolk Road
  9.          Cote St-Luc, Québec
  10.          Canada   H4X 1A3
  11.          (514) 487-1565
  12.          CompuServe: 74646,2157
  13.  
  14.     REVISION: June 1, 1991 - Version 2
  15.                                      Full Colour/Bitmaps
  16.  
  17.                  April 22, 1991 - Version 1.01
  18.                  April 15, 1991 - Version 1
  19.  
  20. *********************************************************************/
  21.  
  22. #include <windows.h>           /* required for all Windows applications */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include "wcdbrk2.h"        /* specific to this program */
  28.  
  29. HANDLE  hInst;                  /* current instance */
  30. HWND      hGameGrid[10][4];   /* array of buttons */
  31. int      board[10][4];        /* array of guesses */
  32. int      code[4];              /* secret code */
  33. char      clues[10][4];        /* array of clues */
  34. int      cur_round;           /* current row */
  35. int     cur_column;         /* current column */
  36. char      str[80];              /* general purpose string */
  37. HWND    DidIGetItRight;
  38. BOOL    Setup;
  39. BITMAP  Bitmap;
  40. HBITMAP hOldBitmap;
  41. HBITMAP hBoardmap;
  42. HBITMAP hBlclpegmap;
  43. HBITMAP hWhclpegmap;
  44. HBITMAP hPegBoardmap;
  45. LPSTR   lpPegs[8] = {"Bluepeg","Dkpupeg","Grenpeg","Ltblpeg",
  46.                              "Oranpeg","Purppeg","Redpeg","Yellpeg"};
  47. HBITMAP hPegs[8];
  48. int iXpos,iYpos;
  49. int curcolour = 0;
  50. int prevcolour = -1;
  51. int winstate;
  52.  
  53. /*********************************************************************/
  54.  
  55. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  56. HANDLE hInstance;                /* current instance */
  57. HANDLE hPrevInstance;         /* previous instance */
  58. LPSTR lpCmdLine;                /* command line */
  59. int nCmdShow;                    /* show-window type (open/icon) */
  60. {
  61.     MSG msg;                       /* message */
  62.  
  63.     Setup = FALSE;
  64.  
  65.     if (!hPrevInstance)                       /* Other instances of app running? */
  66.         if (!InitApplication(hInstance))      /* Initialize shared things */
  67.             return (FALSE);                   /* Exits if unable to initialize */
  68.  
  69.     /* Perform initializations that apply to a specific instance */
  70.  
  71.     if (!InitInstance(hInstance, nCmdShow))
  72.         return (FALSE);
  73.  
  74.  
  75.     /* Aquire and dispatch messages until a WM_QUIT message is recieved */
  76.  
  77.     while (GetMessage(&msg,                  /* message structure */
  78.                             NULL,     /* handle of window recieving the message */
  79.                             NULL,     /* lowest message to examine */
  80.                             NULL))     /* highest message to examine */
  81.     {
  82.         TranslateMessage(&msg);     /* Translates virtual key codes */
  83.         DispatchMessage(&msg);     /* Dispatches message to window */
  84.     }
  85.     return(msg.wParam);  /* Returns the value from PostQuitMessage */
  86. }
  87.  
  88. /*********************************************************************/
  89.  
  90. BOOL InitApplication(hInstance)
  91. HANDLE hInstance;              /* current instance */
  92. {
  93.     WNDCLASS wc;
  94.  
  95.     /* Fill in window class structure with parameters that describe the */
  96.     /* main window. */
  97.  
  98.     wc.style = CS_DBLCLKS;   /* Class style(s). */
  99.     wc.lpfnWndProc = MainWndProc; /* Function to retrieve messages for */
  100.                                             /* windows of this class */
  101.     wc.cbClsExtra = 0;              /* No per-class extra data */
  102.     wc.cbWndExtra = 0;              /* No per-window extra data */
  103.     wc.hInstance = hInstance;       /* Application that owns the class. */
  104.     wc.hIcon = LoadIcon(hInstance, "Wcdbrk2");
  105.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  106.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  107.     wc.lpszMenuName = "WndCdBrkMenu";   /* Name of menu resource in .RC file */
  108.     wc.lpszClassName = "WndCdBrkWClass";/* Name used in call to CreateWindow */
  109.  
  110.     /* Register the window class and return success/failure code */
  111.  
  112.     return (RegisterClass(&wc));
  113.  
  114. }
  115.  
  116. /*********************************************************************/
  117.  
  118. BOOL InitInstance(hInstance, nCmdShow)
  119. HANDLE hInstance;               /* Current instance identifier */
  120. int    nCmdShow;               /* Param for first ShowWindow() call */
  121. {
  122.     HWND hWnd;                   /* Main window handle */
  123.  
  124.     hInst = hInstance;
  125.  
  126.     /* Create a main window for this application instance */
  127.  
  128.     hWnd = CreateWindow(
  129.          "WndCdBrkWClass",         /* See RegisterClass() call */
  130.          "Code Breaker II",      /* Text for window title bar */
  131.          /* The following line creates a window which can be */
  132.          /* minimized to an icon and moved but not re-sized  */
  133.          WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,
  134.          120,                      /* Initial column */
  135.          30,                       /* Initial row    */
  136.          230,                      /* Width          */
  137.          435,                      /* Height         */
  138.          NULL,                        /* Overlapped windows have no parent */
  139.          NULL,                        /* Use the window class menu */
  140.          hInstance,                    /* This instance owns this window */
  141.          NULL                           /* Pointer not needed */
  142.          );
  143.  
  144.     /* If window could not be created, return "failure" */
  145.  
  146.     if (!hWnd)
  147.         return (FALSE);
  148.  
  149.     /* Make the window visible; update its client area; and return "success" */
  150.  
  151.     ShowWindow(hWnd, nCmdShow);     /* Show the window */
  152.     UpdateWindow(hWnd);               /* Sends WM_PAINT message */
  153.  
  154.     return (TRUE);         /* Returns the value from POSTQUITMESSAGE */
  155.  
  156. }
  157.  
  158. /*********************************************************************/
  159.  
  160. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  161. HWND hWnd;                      /* window handle */
  162. unsigned message;               /* type of message */
  163. WORD wParam;                   /* additional information */
  164. LONG lParam;                   /* additional information */
  165. {
  166.     FARPROC     lpProcAbout;           /* pointer to the "About" function */
  167.     int x,y;
  168.  
  169.  
  170.     switch (message)
  171.     {
  172.         case WM_CREATE:
  173.             if (!Setup)    /* Do this only once */
  174.             {
  175.                 hBoardmap   = LoadBitmap(hInst,"Board");
  176.                 hPegBoardmap = LoadBitmap(hInst,"Pegboard");
  177.                 hBlclpegmap = LoadBitmap(hInst,"Blclpeg");
  178.                 hWhclpegmap = LoadBitmap(hInst,"Whclpeg");
  179.                 for (x=0;x<8;x++)
  180.                     hPegs[x] = LoadBitmap(hInst,lpPegs[x]);
  181.                 if(!bMakeGameButtons(hWnd))
  182.                 {
  183.                     MessageBox(hWnd,"Failure to create buttons!",NULL,
  184.                                   MB_OK | MB_ICONHAND);
  185.                 }
  186.                 Setup = TRUE;
  187.             }
  188.             vInitialize();
  189.             vMakecode();
  190.             break;
  191.  
  192.         case WM_LBUTTONUP:          /* Mouse button was pressed */
  193.             iXpos = LOWORD(lParam);
  194.             iYpos = HIWORD(lParam);
  195.             if (bCheckPosition(hWnd))
  196.             {
  197.                 board[cur_round][cur_column] = curcolour;
  198.                 vPutPeg(hWnd,curcolour,cur_round,cur_column);
  199.             }
  200.             break;
  201.  
  202.         case WM_PAINT:              /* Time to repaint */
  203.             vBasicScreen(hWnd);
  204.             vMarkColour(hWnd);
  205.             for (x=0;x<=cur_round;x++)
  206.             {
  207.                 for (y=0;y<4;y++)
  208.                     vPutPeg(hWnd,board[x][y],x,y);
  209.                 vDoClues(hWnd,x);
  210.             }
  211.             if (winstate == 1)
  212.                 vDoWin(hWnd);
  213.             else
  214.                 if (winstate == 2)
  215.                     vDoLoose(hWnd);
  216.  
  217.             break;
  218.  
  219.  
  220.         case WM_COMMAND:         /* message: command from application menu */
  221.             switch(wParam)
  222.             {
  223.                 case DIDIGETITRIGHT:
  224.                     if (!winstate)    /* If won or lost ignore commands */
  225.                     {
  226.                         if (!bCheckfill()) break;
  227.                         if (bCheckguess(cur_round))
  228.                         {
  229.                             vDoWin(hWnd);
  230.                             break;
  231.                         }
  232.                         else
  233.                         {
  234.                             vDoClues(hWnd,cur_round);
  235.                             cur_round++;
  236.                             if (cur_round == 10) /* You loose */
  237.                                 vDoLoose(hWnd);
  238.                         }
  239.                     }
  240.                     break;
  241.  
  242.                 case IDM_ABOUT:
  243.                     lpProcAbout = MakeProcInstance(About, hInst);
  244.  
  245.                     DialogBox(hInst,            /* current instance */
  246.                                  "AboutBox",   /* resource to use */
  247.                                  hWnd,            /* parent handle */
  248.                                  lpProcAbout);    /* About() instance address */
  249.  
  250.                     FreeProcInstance(lpProcAbout);
  251.                     break;
  252.  
  253.                 case IDM_NEW:  /* Let's play again */
  254.                   InvalidateRect(hWnd, NULL, TRUE);
  255.                   SendMessage(hWnd,WM_CREATE,NULL,NULL);
  256.                   break;
  257.  
  258.                 case IDM_GIVEUP:
  259.                     vDoLoose(hWnd);
  260.                     break;
  261.  
  262.                 case IDM_HELP:
  263.                     lpProcAbout = MakeProcInstance(About, hInst);
  264.  
  265.                     DialogBox(hInst,            /* current instance */
  266.                                  "HelpBox",   /* resource to use */
  267.                                  hWnd,            /* parent handle */
  268.                                  lpProcAbout);    /* About() instance address */
  269.  
  270.                     FreeProcInstance(lpProcAbout);
  271.                     break;
  272.  
  273.                 default:               /* Lets Windows pro